﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.Intrinsics.X86;
using System.Security.Policy;
using System.Speech.Recognition;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace AudioDemo
{
    public partial class MainViewModel : ObservableValidator
    {

        /// <summary>
        /// 语音识别结果
        /// </summary>
        [ObservableProperty]
        private bool _recognizedOK = false;


        /// <summary>
        /// 识别已开始
        /// </summary>
        [ObservableProperty]
        private bool _started = false;


        /// <summary>
        /// 识别已结束
        /// </summary>
        [ObservableProperty]
        private bool _completed = false;


        /// <summary>
        /// 识别关键字
        /// </summary>
        [ObservableProperty]
        private string _keyWords = "K113,B/1道停车,引导";


        /// <summary>
        /// 语音播放文字
        /// </summary>
        [ObservableProperty]
        private string _strForPlay = "客车K113,B号站引导接车1道停车,注意引导信号";


        /// <summary>
        /// 用于播放的音频文件路径
        /// </summary>
        [ObservableProperty]
        private string _audioFile = "C:\\Users\\Administrator\\Desktop\\RTSS考评标准站\\考评系统及环境包\\KPstudent\\电话铃声.wav";


        /// <summary>
        /// 音频处理
        /// </summary>
        private AudioHelper audioHelper = new AudioHelper();


        /// <summary>
        /// 识别按钮回调
        /// </summary>
        [RelayCommand]
        public async void SreBtn(string cmd)
        {
            try
            {
                if (cmd == "1001")
                {
                    bool result;
                    audioHelper.Speak("开始语音识别");
                    Started = true;
                    Completed = false;
                    RecognizedOK = false;


                    audioHelper.Speak("请靠近麦克风说话");
                    DispatcherHelper.DoEvents();

                    RecognizedOK = audioHelper.RecognizeKeyWords(KeyWords,600);
                    Completed = true;
                }
                else if (cmd == "999")
                {
                    audioHelper.StopRecognize();
                    Completed = true;
                }               
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }


        /// <summary>
        /// 播放文字按钮回调
        /// </summary>
        [RelayCommand]
        public void SpeekBtn(string cmd)
        {
            try
            {
                if (!string.IsNullOrEmpty(StrForPlay))
                    audioHelper.Speak(StrForPlay);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }


        /// <summary>
        /// 播放声音文件按钮回调
        /// </summary>
        [RelayCommand]
        public void PlayFileBtn(string cmd)
        {
            try
            {
                if (File.Exists(AudioFile))
                    audioHelper.PlaySoundFile(AudioFile);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }


        /// <summary>
        /// 选择文件按钮回调
        /// </summary>
        [RelayCommand]
        public void SelectFileBtn(string cmd)
        {
            try
            {
                var openFileDialog = new Microsoft.Win32.OpenFileDialog()
                {
                    Filter = "wav Files (.wav)|*.wav|All files (*.*)|*.*"
                };
                var result = openFileDialog.ShowDialog();
                if (result == true)
                {
                    AudioFile= openFileDialog.FileName;
                }
                else
                {
                    AudioFile= null;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}